﻿using System;
using System.Linq;
using System.Web.UI.HtmlControls;
using eCommerce.DataAccess;

namespace eCommerce.Site
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
 
            //txbSearch.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + imglnkbtnSearch.UniqueID + "').click();return false;}} else {return true}; ");
            //Page.RegisterHiddenField("__EVENTTARGET", imglnkbtnSearch.UniqueID);
            //txbSearch.Attributes.Add("onkeypress", "return clickButton(event,'" + imglnkbtnSearch.ClientID + "')");
            if (IsPostBack) return;
            WebsiteDataContext db = new WebsiteDataContext();
            string pagespecial = "/Default.aspx";
            pagespecial = Request.Url.PathAndQuery.ToString();
            //Response.Write(pagespecial);

            string metaKeywords = "";
            string metaDescription = "";
            string metaTitle = "";

            Xcms_General general = (from b in db.Xcms_Generals
                                    select b).FirstOrDefault();

            Xcms_Page pages = (from p in db.Xcms_Pages
                               where p.pagespecial == pagespecial
                               select p).FirstOrDefault();
            if (pages == null)
            {
                metaKeywords = general.generalmetatags.ToString();
                metaDescription = general.generalmetadescription.ToString();
                metaTitle = general.generaltitle.ToString();
            }
            else
            {
                metaKeywords = pages.pagemetatags != ""
                                   ? pages.pagemetatags.ToString()
                                   : general.generalmetatags.ToString();
                metaDescription = pages.pagemetadescription != ""
                                      ? pages.pagemetadescription.ToString()
                                      : general.generalmetadescription.ToString();
                metaTitle = pages.pagetitle != "" ? pages.pagetitle.ToString() : general.generaltitle.ToString();
            }

            if (general == null)
            {
                metaKeywords = "Ecommerce, Content Management System, 2beknown.co.uk, 2beknown ecommerce, design and development";
                metaDescription = "Ecommerce website including content management system";
                metaTitle = "2beknown Ecommerce - Making online payments posible the easy way";
            }

            //Insert Title
            this.headscript.Title = metaTitle;

            // Render: <meta name="keywords" content="Some words listed here" />
            HtmlMeta meta = new HtmlMeta();
            meta.Name = "keywords";
            meta.Content = "" + metaKeywords + "";
            this.headscript.Controls.Add(meta);

            // Render: <meta name="keywords" content="Some words listed here" />
            meta = new HtmlMeta();
            meta.Name = "description";
            meta.Content = "" + metaDescription + "";
            this.headscript.Controls.Add(meta);

            // Render: <meta name="robots" content="noindex" />
            //meta = new HtmlMeta();
            //meta.Name = "robots";
            //meta.Content = "noindex";
            //this.headscript.Controls.Add(meta);

            // Render: <meta name="date" content="2006-03-25" scheme="YYYY-MM-DD" />
            meta = new HtmlMeta();
            meta.Name = "date";
            meta.Content = DateTime.Now.ToString("dd-MM-yyyy");
            meta.Scheme = "DD-MM-YYYY";
            this.headscript.Controls.Add(meta);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            
            CartDA x = new CartDA();
            if (SiteFunctions.CookiesEnabled)
            {
                if (Session["cartid"] != null)
                {
                    //items.Text = "<span class=\"wish\" title=\"" + Session["cartid"].ToString() + "\">";
                    items.Text = "<span class=\"wish\" title=\"Shopping Basket\">";
                    if (x.ProductsQty > 0)
                    {
                        if (x.ProductsQty > 10)
                        {
                            items.Text += "<span class=\"wish\" style=\"margin:0px 0px 0px 0px;\">(" + x.ProductsQty + ")</span>";
                        }
                        else
                        {
                            items.Text += "<span class=\"wish\" style=\"margin:0px 0px 0px 0px;\">(" + x.ProductsQty + ")</span> ";
                        }
                        //items.Text += x.ProductsQty + " Item";
                        //if (x.ProductsQty > 1)
                        //{
                        // items.Text += "s";
                        //}
                        //items.Text += ", " + String.Format("{0:C}", x.Total);  // No need for price value
                    }
                    else
                    {
                        //items.Text += "Empty - " + Session["cartid"];
                        items.Text += " <span class=\"wish\" style=\"margin:0px 0px 0px 0px;\">(0)</span> ";
                    }
                    //sesionid.Text = Session["cartid"].ToString();
                    items.Text += "</span>";
                }
                else
                {
                    items.Text = "<span class=\"wish\" title=\"Invalid Cart!\">";
                    items.Text += " <span class=\"wish\" style=\"margin:0px 0px 0px 0px;\">&nbsp;</span> ";
                    items.Text += "</span>";
                   
                }
            }
            else
            {
                items.Text = "<span class=\"wish\" title=\"No Cookie!\">";
                items.Text += " <span class=\"wish\" style=\"margin:0px 0px 0px 0px;\">(e)</span> ";
                items.Text += "</span>";
            }
        }
        protected void searchbtn_Click(object sender, EventArgs e)
        {
            String str = SiteFunctions.RemoveDoubleSpaces(txbSearch.Text).Replace(" ", "+");
            Response.Redirect("~/Search.aspx?s=" + str);
        }



       
    }
}
